package guda.stark.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.stark.biz.ContactBiz;
import guda.stark.dal.dao.ContactDAO;
import guda.stark.dal.dataobject.ContactDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class ContactBizImpl implements ContactBiz{

    private final static Logger logger = LoggerFactory.getLogger(ContactBizImpl.class);

    @Autowired
    private ContactDAO contactDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ContactDO contactDO = contactDAO.getContactById(id);
            bizResult.data.put("contactDO", contactDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Contact error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = contactDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<ContactDO> contactList = contactDAO.selectForPage(baseQuery);
            bizResult.data.put("contactList", contactList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Contact list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            contactDAO.delContactById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete contact error", e);
        }
        return bizResult;
    }

    public BizResult create(ContactDO contactDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = contactDAO.insert(contactDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Contact error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(ContactDO contactDO) {
        BizResult bizResult = new BizResult();
        try {
            contactDAO.updateByIdSelective(contactDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Contact error", e);
        }
        return bizResult;
    }


}
